package com.biz.primus.ms.stock.dao.ro;


import com.biz.primus.base.redis.annotation.RedisWriteIgnore;
import com.biz.primus.base.redis.annotation.Ro;
import com.biz.primus.base.redis.bean.BaseRedisObject;
import com.biz.primus.common.enums.StockChannelEnum;
import com.biz.primus.common.utils.DateUtil;
import com.biz.primus.model.stock.vo.AllStockItemVo;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 门店库存 全量
 * <p>
 * redisKey = stock:depot:${channel}:${depotCode}:${productCode}
 *
 * @author lei
 */
@Ro(key = "stock:depot")
@Getter
@Setter
@NoArgsConstructor
public class DepotStockRo extends BaseRedisObject<String> {

    private static final long serialVersionUID = 6461449450948278633L;

    /**
     * 门店编号
     **/
    private String depotCode;

    /**
     * sku编码
     */
    private String productCode;

    /**
     * 渠道码
     */
    private StockChannelEnum channelCode;

    /**
     * 当前账面库存
     */
    @RedisWriteIgnore(type = RedisWriteIgnore.IgnoreType.NULL)
    private Integer quantity;

    /**
     * 锁定库存
     */
    @RedisWriteIgnore(type = RedisWriteIgnore.IgnoreType.NULL)
    private Integer lockedQuantity;

    /**
     * 版本
     */
    @RedisWriteIgnore(type = RedisWriteIgnore.IgnoreType.NULL)
    private Long version;

    /**
     * 初始化时间
     */
    private String initTime;

    public DepotStockRo(StockChannelEnum channelCode, String depotCode, String initTime, String productCode, Integer quantity, Long version) {
        this(channelCode, depotCode, initTime, productCode, version);
        this.quantity = quantity == null ? 0 : quantity > 0 ? quantity : 0;
    }

    public DepotStockRo(StockChannelEnum channelCode, String depotCode, String initTime, String productCode, Long version) {
        this.setId(String.format("%s%s%s%s%s", channelCode, Constants.SEPARATOR, depotCode, Constants.SEPARATOR, productCode));
        this.depotCode = depotCode;
        this.productCode = productCode;
        this.version = version == null ? -1L : version;
        this.lockedQuantity = 0;
        this.initTime = initTime;
        this.quantity = 0;
        this.setCreateTimestamp(DateUtil.now());
    }

    public DepotStockRo(AllStockItemVo vo, Long v) {
        this(vo.getChannelCode(), vo.getDepotCode(), vo.getInitTime(), vo.getProductCode(), v);
    }
}
